package com.yc.dao;

import com.yc.bean.OrderBean;
import com.yc.commons.DBHelper;
import lombok.Data;

import java.util.List;

@Data
public class OrderDao {
    static DBHelper db = new DBHelper();

    public static int setOrderList(int userid, String order_code, Integer table_id, double total_Price) {
        //订单的创建
        String sql = "INSERT INTO tb_order (userid,order_code, table_id, total_Price, order_Status, order_date, update_date)\n" +
                "VALUES (?,?, ?, ?, 0, NOW(), NOW())";

        return db.doUpdateKey(sql, userid, order_code, table_id, total_Price);

    }

    public static List<OrderBean> find(int userid) {
        String sql = "SELECT id, order_code, table_id, total_price, order_status, order_date " +
                "FROM tb_order " +
                "WHERE disabled = 0 AND userid = ? " +
                "ORDER BY " +
                "CASE WHEN order_status = 1 THEN 1 ELSE 0 END, " +  // order_status 为 1 排前面，为 0 排后面
                "order_date DESC;";


        try {
            return db.select(OrderBean.class, sql, userid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Boolean update(String order_code) {
        String sql = "UPDATE tb_order\n" +
                "SET order_status=1 ,pay_date=NOW()\n" +
                "WHERE order_code=?;\n";

        try {
            int i = db.doUpdate(sql, order_code);
            if (i>0){
                return true;
            }
            return false;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}